package com.fcgzs.springboot.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fcgzs.springboot.mapper.MenuMapper;
import com.fcgzs.springboot.mapper.PermissionMapper;
import com.fcgzs.springboot.mapper.RoleMapper;
import com.fcgzs.springboot.mapper.UserMapper;
import com.fcgzs.springboot.pojo.Permission;
import com.fcgzs.springboot.pojo.Role;
import com.fcgzs.springboot.pojo.User;
import com.fcgzs.springboot.service.UserService;
import com.fcgzs.springboot.util.Result;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Component
@Service
public class UserServiceImpl implements UserService {

    @Resource
    private UserMapper userMapper;
    @Resource
    private RoleMapper roleMapper;
    @Resource
    private PermissionMapper permissionMapper;
    @Resource
    private MenuMapper menuMapper;

    //通过用户名查询用户
    @Override
    public User getUserByUserName(String username) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username",username);
        User user = userMapper.selectOne(queryWrapper);
        return user;
    }
    //通过用户id查询用户对应的角色信息
    @Override
    public List<Role> getRoleByUid(Integer uid) {
        return roleMapper.getRoleByUid(uid);
    }
    //通过用户id查询资源信息
    @Override
    public List<Permission> getPermissionByUid(Integer uid) {
        return permissionMapper.getPermissionByUid(uid);
    }
    //通过用户名获取菜单
    @Override
    public Result getMenuByUname(String username) {
        User userByUserName = getUserByUserName(username);
        //一级菜单
        List<Map<String,Object>> list = menuMapper.getParentMenuByUid(userByUserName.getId());
        //二级菜单
        for (Map<String, Object> one : list) {
            List<Map<String,Object>> childList = menuMapper.getChildMenuByUidAndPid(userByUserName.getId(),(Integer) one.get("id"));
            one.put("children",childList);
        }
        return new Result(true,"",list);
    }
}